/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.aptech.mBean;

import com.aptech.entities.TblOrder;
import com.aptech.entities.TblProject;
import com.aptech.mBean.util.JsfUtil;
import com.aptech.mBean.util.PaginationHelper;
import com.aptech.sBean.TblProjectFacade;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;
import javax.faces.model.SelectItem;
import org.primefaces.event.RowEditEvent;

/**
 *
 * @author LanhAnh
 */
@ManagedBean(name = "projectController")
@SessionScoped
public class ProjectController implements Serializable {

    @EJB
    private TblProjectFacade tblProjectFacade;
    private DataModel items = null;
    private PaginationHelper paginationHelper;
    private TblProject current;
    private int selectedItemIndex;
    private List<TblProject> listManagerProject;

    public List<TblProject> getListManagerProject(int mnID) {
        return tblProjectFacade.getProjectByManagerID(mnID);
    }

    public List<TblProject> getProjectByStatus1() {
        return tblProjectFacade.getProjectByStatus1();
    }

    public List<TblProject> getProjectByStatus2() {
        return tblProjectFacade.getProjectByStatus2();
    }

    /**
     * Creates a new instance of ProjectController
     */
    public ProjectController() {
    }

    public TblProject getSelected() {
        if (current == null) {
            current = new TblProject();
            selectedItemIndex = -1;
        }
        return current;
    }

    public TblProjectFacade getFacade() {
        return tblProjectFacade;
    }

    public PaginationHelper getPaginationHelper() {
        if (paginationHelper == null) {
            paginationHelper = new PaginationHelper(10) {
                @Override
                public int getItemsCount() {
                    return getFacade().count();
                }

                @Override
                public DataModel createPageDataModel() {
                    return new ListDataModel(getFacade().findRange(new int[]{getPageFirstItem(), getPageFirstItem() + getPageSize()}));
                }
            };
        }
        return paginationHelper;
    }

    public DataModel getItems() {
        if (items == null) {
            items = getPaginationHelper().createPageDataModel();
        }
        return items;
    }

    private void recreateModel() {
        items = null;
    }

    private void recreatePagination() {
        paginationHelper = null;
    }

    public String prepareView(TblProject b) {
        current = b;
        return "ProjectDetailManager";
    }

    public String prepareList() {
        recreateModel();
        return "ProjectManagement";
    }

    public void onEdit(RowEditEvent e) {
        FacesMessage msg = new FacesMessage("Edited Project", ((TblProject) e.getObject()).getPrID().toString());
        FacesContext.getCurrentInstance().addMessage(null, msg);
        getFacade().edit((TblProject) e.getObject());
    }

    public void onEditCancel(RowEditEvent e) {
        FacesMessage msg = new FacesMessage("Edit Project Cancelled", ((TblProject) e.getObject()).getPrID().toString());
        FacesContext.getCurrentInstance().addMessage(null, msg);
    }

    public SelectItem[] getItemsAvailableSelectMany() {
        return JsfUtil.getSelectItems(tblProjectFacade.findAll(), false);
    }

    public SelectItem[] getItemsAvailableSelectOne() {
        return JsfUtil.getSelectItems(tblProjectFacade.findAll(), true);
    }

    public String prepareCreate() {
        current = new TblProject();
        selectedItemIndex = -1;
        return "ProjectManagement";
    }

    public String create() {
        try {
            getFacade().create(current);
            return prepareCreate();
        } catch (Exception e) {
            return null;
        }
    }

    @FacesConverter(forClass = TblProject.class)
    public static class ProjectControllerConverter implements Converter {

        @Override
        public Object getAsObject(FacesContext context, UIComponent component, String value) {
            if (value == null || value.length() == 0) {
                return null;
            }
            ProjectController controller = (ProjectController) context.getApplication().getELResolver().
                    getValue(context.getELContext(), null, "projectController");
            return controller.tblProjectFacade.find(getKey(value));
        }

        @Override
        public String getAsString(FacesContext context, UIComponent component, Object object) {
            if (object == null) {
                return null;
            }
            if (object instanceof TblProject) {
                TblProject o = (TblProject) object;
                return getStringKey(o.getPrID());
            } else {
                throw new IllegalArgumentException("object " + object + " is of type " + object.getClass().getName() + "; expected type: " + TblProject.class.getName());
            }
        }

        java.lang.Integer getKey(String value) {
            java.lang.Integer key;
            key = Integer.valueOf(value);
            return key;
        }

        String getStringKey(java.lang.Integer value) {
            StringBuffer sb = new StringBuffer();
            sb.append(value);
            return sb.toString();
        }
    }
    //Statistic
    private Date dateStart = new Date(110, 01, 01);
    private Date dateEnd = new Date();
    private int dateType = 0;

    public int getDateType() {
        return dateType;
    }

    public void setDateType(int dateType) {
        this.dateType = dateType;
    }

    

    public Date getDateStart() {
        return dateStart;
    }

    public void setDateStart(Date dateStart) {
        this.dateStart = dateStart;
    }

    public Date getDateEnd() {
        return dateEnd;
    }

    public void setDateEnd(Date dateEnd) {
        this.dateEnd = dateEnd;
    }
    private DataModel itemsStt = null;

    public DataModel getItemsStt() {
        if (itemsStt == null) {
            if (dateType==0) {
                itemsStt = sttGetByDateCreated();
            } else {
                itemsStt = sttGetByDateCompleted();
            }
        }
        return itemsStt;
    }

    public DataModel prepareItemsStt() {
        if (dateType==0) {
            itemsStt = sttGetByDateCreated();
        } else {
            itemsStt = sttGetByDateCompleted();
        }
        return itemsStt;
    }

    public void setItemsStt(DataModel itemsStt) {
        this.itemsStt = itemsStt;
    }

    public DataModel sttGetByDateCreated() {

        itemsStt = new ListDataModel(getFacade().getTblProjectByDateCreated(dateStart, dateEnd));
        return itemsStt;
    }

    public DataModel sttGetByDateCompleted() {

        itemsStt = new ListDataModel(getFacade().getTblProjectByDateCompleted(dateStart, dateEnd));
        return itemsStt;
    }

    public String sttView(TblProject c) {
        current = c;
        return "/Admin/Project/ProjectDetailManager";
    }
    
    public String sttViewByOrder(TblOrder o){
        current=getFacade().getTblProjectByOdID(o.getOdID()).get(0);
        return "/Admin/Project/ProjectDetailManager";
    }
    public boolean sttCheckProjectWithOrder(TblOrder o){
        return getFacade().getTblProjectByOdID(o.getOdID()).isEmpty();
    }
    
    public SelectItem[] getItemState(){        
        SelectItem[] selectItems=new SelectItem[6];
        selectItems[0]=new SelectItem("", "Select One");
        selectItems[1]=new SelectItem(1, "Chua bat dau");
        selectItems[2]=new SelectItem(2, "Dang duoc thuc hien");
        selectItems[3]=new SelectItem(3, "Dang can bao tri");
        selectItems[4]=new SelectItem(4, "Dang duoc bao tri");
        selectItems[5]=new SelectItem(5, "Da hoan thien");
        return selectItems;
    }
}
